import java.io.File;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/*
ID:luongho3
LANG:JAVA
TASK:prime3
 */
public class prime3 {

	public static int[] isPrime = new int[100000];
	public static int[] primes = new int[100000];
	public static int[] starts = new int[100000];
	public static int nPrimes, S, topDigit, nToStart, nSol;
	public static int[][] table = new int[6][6];
	public static PriorityQueue<mTable> pq;
	
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(new File("prime3.in"));
		PrintStream ps = new PrintStream(new File("prime3.out"));
		System.setOut(ps);
		
		S = scanner.nextInt();
		topDigit = scanner.nextInt();

 		sieve();

		extractPrimes();
		
		if (nPrimes ==0 || nToStart == 0) {
			System.out.println("NONE");
		}
		else {
			nSol = 0;	
			pq = new PriorityQueue<mTable>(100, new Comparator<mTable>() {

				@Override
				public int compare(mTable a, mTable b) {
					for (int i=1;i<=5;++i) {
						for (int j=1;j<=5;++j) {
							if (a.table[i][j] != b.table[i][j]) {
								return a.table[i][j] - b.table[i][j];
							}
						}
					}
					return 0;
				}
				
			});
			for (int i=1;i<=nToStart;++i) {
				for (int j=1;j<=nToStart;++j) {
					for (int k=1;k<=nToStart;++k) {
						int r1 = starts[i];
						int c1 = starts[j];
						int d1 = starts[k];
//						if (i == 2 && j == 3 && k == 22) {
//							System.out.println("Here");
//						}
						if (checkValidInit(r1,c1)) {
							initTable(r1,c1,d1);
							make_try();
						}
					}
				}
			}
			
			while (!pq.isEmpty()) {
				table = pq.poll().table;
				printTable();
				if (!pq.isEmpty()) {
					System.out.println();
				}
			}
		}
		ps.close();
	}
	
	private static boolean checkValidInit(int a, int b) {
		for (int i=1;i<=5;++i) {
			int tmp = a % 10;
			a /= 10;
			if (tmp == 0) 
				return false;
			
			tmp = b % 10;
			b /= 10;
			if (tmp == 0) 
				return false;
		}
		return true;
	}


	private static void make_try() {
		try_d();
	}


	private static void try_last() {
		table[5][4] = S - table[1][4] - table[2][4] - table[3][4] - table[4][4];
		table[4][5] = S - table[4][1] - table[4][2] - table[4][3] - table[4][4];

		if (table[5][4] >= 0 && table[4][5] >= 0 && table[5][4] <= 9 && table[4][5] <= 9) {
			if (checkPrimeRow(4)) {
				if (checkPrimeCol(4)) {
					if (sumRow(5) == S) {
						if (checkPrimeRow(5)) {
							if (sumCol(5) == S) {
								if (checkPrimeCol(5)) {
									nSol++;
//									if (nSol > 1) {
//										System.out.println();
//									}
									pq.add(new mTable(table));
								}
							}
						}
					}		
				}
			}
		}
		
	}

	private static boolean checkPrimeRow(int r) {
		int num = 0;
		for (int i=1;i<=5;++i) {
			num *= 10;
			num += table[r][i];
		}
		return (isPrime[num] != -1);
	}
	
	private static boolean checkPrimeCol(int c) {
		int num = 0;
		for (int i=1;i<=5;++i) {
			num *= 10;
			num += table[i][c];
		}
		return (isPrime[num] != -1);
	}
	
	private static int sumRow(int r) {
		int sum = 0;
		for (int i=1;i<=5;++i) {
			sum += table[r][i];
		}
		return sum;
	}
	
	private static int sumCol(int c) {
		int sum = 0;
		for (int i=1;i<=5;++i) {
			sum += table[i][c];
		}
		return sum;
	}

	private static void try_c(int c) {
		if (c == 2) {
			for (int i=0;i<=9;++i) {
				table[3][2] = i;
				table[5][2] = S - table[1][2] - table[2][2] - table[3][2] - table[4][2];
				if (table[3][2] >= 0 && table[5][2] >= 0 && table[3][2] <= 9 && table[5][2] <= 9) {
					int num = table[1][2] * 10000 + table[2][2] * 1000 + table[3][2] * 100 + table[4][2] * 10 + table[5][2];
					if (isPrime[num] != -1) {
						try_r(3);
					}
				}
			}
		}
		else if (c==3) {
			for (int i=0;i<=9;++i) {
				table[4][3] = i;
				table[5][3] = S - table[1][3] - table[2][3] - table[3][3] - table[4][3];
				if (table[4][3] >= 0 && table[5][3] >= 0 && table[4][3] <= 9 && table[5][3] <= 9) {
					int num = table[1][3] * 10000 + table[2][3] * 1000 + table[3][3] * 100 + table[4][3] * 10 + table[5][3];
					if (isPrime[num] != -1) {
						try_last();
					}
				}
			}
		}
//		
//		if (i==2) {
//			try_r(3);
//		}
//		else if (i==3) {
//			try_last();
//		}
	}


	private static void try_r(int r) {
		if (r == 2) {
			for (int i=0;i<=9;++i) {
				table[2][3] = i;
				table[2][5] = S - table[2][3] - table[2][1] - table[2][2] - table[2][4];
				if (table[2][3] >= 0 && table[2][5] >= 0 && table[2][3] <= 9 && table[2][5] <= 9) {
					int num = table[2][1] * 10000 + table[2][2] * 1000 + table[2][3] * 100 + table[2][4] * 10 + table[2][5];
					if (isPrime[num] != -1) {
						try_c(2);
					}
				}
			}
		}
		else if (r==3) {
			for (int i=0;i<=9;++i) {
				table[3][4] = i;
				table[3][5] = S - table[3][1] - table[3][2] - table[3][3] - table[3][4];
				if (table[3][4] >= 0 && table[3][5] >= 0 && table[3][4] <= 9 && table[3][5] <= 9) {
					int num = table[3][1] * 10000 + table[3][2] * 1000 + table[3][3] * 100 + table[3][4] * 10 + table[3][5];
					if (isPrime[num] != -1) {
						try_c(3);
					}
				}
			}
		}
	}


	private static void try_d() {
		for (int i=0;i<=9;++i) {
			table[4][2] = i;
			table[2][4] = S - table[5][1] - table[4][2] - table[3][3] - table[1][5];
			if (table[4][2] >= 0 && table[2][4] >= 0 && table[4][2] <= 9 && table[2][4] <= 9) { 
				int num = table[5][1] * 10000 + table[4][2] * 1000 + table[3][3] * 100 + table[2][4] * 10 + table[1][5];
				if (isPrime[num] != -1) {
					try_r(2);
				}
			}
		}
	}


	private static void printTable() {
		for (int i=1;i<=5;++i) {
			for (int j=1;j<=5;++j) {
				System.out.print(table[i][j]);
			}
			System.out.println();
		}
	}

	private static void initTable(int r1, int c1, int d1) {
				table[1][1] = topDigit;
		for (int i=5;i>=2;--i) {
			table[1][i] = r1 % 10;
			r1 /= 10;
		}
		for (int i=5;i>=2;--i) {
			table[i][1] = c1 % 10;
			c1 /= 10;
		}
		for (int i=5;i>=2;--i) {
			table[i][i] = d1 % 10;
			d1 /= 10;
		}
	}

	private static void extractPrimes() {
		nPrimes = 0;
		nToStart = 0;
		for (int i=10000;i<100000;++i) {
			if (isPrime[i] != -1) {
				if (dSum(i) == S) {
					nPrimes++;
					primes[nPrimes] = i;
					if ((i / 10000) == topDigit) {
						nToStart++;
						starts[nToStart] = i;
//						if (i==23879) {
//							System.out.println(nToStart);
//						}
//						if (i==24977) {
//							System.out.println(nToStart);
//						}
//						if (i==28289) {
//							System.out.println(nToStart);
//						}
					}
				}
			}
		}
	}

	private static int dSum(int n) {
		int sum = 0;
		while (n > 0) {
			sum += (n % 10);
			n /= 10;
		}
		return sum;
	}

	private static void sieve() {
		
		isPrime[0] = isPrime[1] = -1;
		for (int i=2;i*i<100000;++i) {
			if (isPrime[i] == 0) {
				isPrime[i] = 1;
				int j = 2;
				while (i*j < 100000) {
					isPrime[i*j] = -1;
					j++;
				}
			}
		}
	}

}

class mTable {
	int[][] table = new int[6][6];
	mTable(int[][] table) {
		for (int i=1;i<=5;++i) {
			for (int j=1;j<=5;++j) {
				this.table[i][j] = table[i][j];
			}
		}
	}
}
